import axios from 'axios';

let env = process.env.NODE_ENV; // 环境变量
let httpOption = {}; // 基础axios配置

if(env === 'development') { // 开发环境 代理
  httpOption = {
    baseURL: "/api",
    headers: {
      "Content-Type": "application/json;charset=UTF-8"
    }
  };
} else { // 生产环境
  httpOption = {
    baseURL: "http://localhost:4000",
    headers: {
      "Content-Type": "application/json;charset=UTF-8"
    }
  };
}
  
// 创建axios实例
const http = axios.create({
  baseURL: httpOption.baseURL,
  headers: httpOption.headers,
  timeout: 60000
});

// 拦截器
http.interceptors.request.use(
  config => {
    return config;
  },
  err => {
    return Promise.reject(err)
  }
);
http.interceptors.response.use(
  res => {
    return res.data;
  },
  err => {
    return Promise.reject(err)
  }
);

// 请求函数
export default (method, url, data=null) => {
  if(method == 'get') {
    return http({
      method,
      url,
      params: data
    })
  } else if(method == 'post') {
    return http({
      method,
      url,
      data,
    })
  }
};

